from ..database import database, TABLE_PREFIX
from  peewee import *
from .member import Member
from .product import Product
from .service import Service
from playhouse.fields import ManyToManyField
from .offer import Offer
import datetime


class ShopCart(database.Model):
    id = CharField(db_column='id', max_length=60, primary_key=True)
    member = ForeignKeyField(Member, null=True)
    created_at = DateTimeField(db_column='created_at', help_text='创建时间', default=datetime.datetime.now)
    updated_at = DateTimeField(db_column='updated_at', help_text='更新时间')
    status = CharField(db_column='status', max_length=20, help_text='状态', default='created')

    # class Meta:
    #     db_table = '{}_shop_carts'.format(TABLE_PREFIX)

    @classmethod
    def get_by_member(cls, member):
        return cls.get_or_none(
            (cls.member == member) &
            (cls.status == 'created')
        )

    @property
    def is_normal(self):
        return 'created' == self.status

    @property
    def items(self):
        return CartItem.select().where(CartItem.shop_cart == self).where(CartItem.status == 'normal')


class CartItem(database.Model):
    id = PrimaryKeyField(db_column='id')
    product = ForeignKeyField(Product)
    stock = ForeignKeyField(Offer)

    shop_cart = ForeignKeyField(ShopCart, null=True)
    count = IntegerField(db_column='count', default=0)

    created_at = DateTimeField(db_column='created_at', help_text='创建时间', default=datetime.datetime.now)
    status = CharField(db_column='status', max_length=20, help_text='状态', default='normal')
    source = CharField(db_column='source', max_length=100, help_text='来源系统')



    @classmethod
    def get_by_source(cls,source):
        return  cls.select().where(
            (cls.source  == source) & (cls.status == 'normal')
        ).order_by(cls.created_at)
    
    @classmethod
    def get_item_from(cls, source, product, stock):
        return cls.get_or_none(
            (cls.source == source) &
            (cls.product == product) &
            (cls.stock == stock) &
            (cls.status == 'normal')
        )

    @classmethod
    def get_available_item(cls, shop_cart, product, stock):
        return cls.get_or_none(
            (cls.shop_cart == shop_cart) & (cls.product == product) & (cls.stock == stock) & (
                cls.status == 'normal'))


class CartItemAdditive(database.Model):
    '''
        购物车条目的附加项。
        可附加 ： service - 服务
    '''
    id = PrimaryKeyField(db_column='id')
    title = CharField(db_column='title', help_text='标题', max_length=80)
    description = CharField(db_column='description', help_text='描述', null=True
                            , max_length=200)
    type = CharField(db_column='type', help_text='附加项类型', max_length=20)
    ref_id = CharField(db_column='ref_id', help_text='附加项引用Id', max_length=80)
    required = CharField(db_column='required', help_text='是否必须附加，不可取消', max_length=1, default='Y')
    cart_item = ForeignKeyField(CartItem, related_name='additives')

    # class Meta:
    #     db_table = '{}_cart_item_additives'.format(TABLE_PREFIX)

    @classmethod
    def delete_all_for(cls, cart_item):
        cls.delete().where(cls.cart_item == cart_item).execute()
